/**
 * @author meyoo
 * @date 2018/3/28
 * @Description:
 */
var MongooseAsync = require("../../core/MongooseAsync.js")

class DatabaseManager {
    //初始化
    constructor() {


    }

    static instance() {
        if (DatabaseManager.g_instance == null) {
            DatabaseManager.g_instance = new DatabaseManager();
        }
        return DatabaseManager.g_instance;
    }

    async initDB(account,password,ip,port,dbname) {
        this.mog = new MongooseAsync();

        var isok =  this.mog.connect(account,password,ip,port,dbname);
        if(isok){
             this.mog.makeModel("userinfo",{id:Number,account:String,pass:String,logintime:Date,score:Number});

            //链接成功 定义表
            return Promise.resolve(true);
        }
        return Promise.resolve(false);
    }
    async createPlayer(id,account,pass,score){
        var userinfoModel = this.mog.getModle("userinfo");
        var newPlayer = new userinfoModel();
        newPlayer.id = id;
        newPlayer.account = account;
        newPlayer.pass = pass;
        newPlayer.score = score;
        newPlayer.logintime = new Date();
        return await newPlayer.save();

    }
    async findPlayer(account){
        var userinfoModel = this.mog.getModle("userinfo");
        var infos = await userinfoModel.find({account:account});
        return Promise.resolve(infos);
    }

}

DatabaseManager.g_instance = null;

module.exports =  DatabaseManager;